#! /usr/bin/python3
from requests import Request, Session
from eggs.sanbayaccount import SB_USER, SB_PWD
import threading
import csv
import sys
import datetime

login_url = 'https://www.shanbay.com/api/v1/account/login/web/'


get_familiar_words = 'https://www.shanbay.com/api/v1/bdc/library/familiar/?page=%s&_=1493170715107'
get_master_words = 'https://www.shanbay.com/api/v1/bdc/library/master/?page=%s&_=1493170715107'
get_fresh_words = 'https://www.shanbay.com/api/v1/bdc/library/fresh/?page=%s&_=1493170715107'
get_today_words = 'https://www.shanbay.com/api/v1/bdc/library/today/?page=%s&_=1493170715107'
get_resolved_words = 'https://www.shanbay.com/api/v1/bdc/library/resolved/?page=%s&_=1558105833641'

# 分别为 正在学习，已掌握单词，新的单词，今日单词,简单词
words_urls = ['https://www.shanbay.com/api/v1/bdc/library/familiar/?page=%s',
              'https://www.shanbay.com/api/v1/bdc/library/master/?page=%s',
              'https://www.shanbay.com/api/v1/bdc/library/fresh/?page=%s',
              'https://www.shanbay.com/api/v1/bdc/library/today/?page=%s',
              'https://www.shanbay.com/api/v1/bdc/library/resolved/?page=%s'
             ]

s = Session()
req = Request('PUT', login_url, data={"username": SB_USER, "password": SB_PWD})

prepped = s.prepare_request(req)

resp = s.send(prepped)

#print(resp.text)

#if not login():
#    sys.exit(0)


def login():
    req = Request('PUT',  login_url, data={"username": SB_USER, "password": SB_PWD})
    prepped = sanbay.prepare_request(req)
    resp = sanbay.send(prepped)
    if resp.msg == "SUCCESS":
        return True
    else:
        print('login failed.')
        print(resp.msg)
        return False


def get_wordlist(url):
    pass

def get_wordpages(url, ):
    pass


def parse_wordpage(url):
    pass

def save_word(file, words):
    pass

def build_word_list_urls(session, url_templets):
    for tmp in url_templets:
        first = s.send(s.prepare_request(Request('GET', tmp % 1))).json()
        page_count = (first["data"]["total"] // 10) + 1
        for i in range(1, page_count + 1):
            yield tmp % i


def export_my_word_list(session, url, words):

    learn_resp = session.send(s.prepare_request(Request('GET', url)))
    resp_words = learn_resp.json()

    for w in resp_words["data"]["objects"]:
        # yield w["content"].lower()
        words.append(w["content"].lower())




def export_my_words(filename, wurl):

    with open(filename, 'w') as csvfile:
        fieldnames = ['content', 'definition']
        writer = csv.DictWriter(csvfile, fieldnames=fieldnames, extrasaction='ignore')
        writer.writeheader()

        first = s.send(s.prepare_request(Request('GET', wurl % 1))).json()

        page_total = (first["data"]["total"] // 10) + 1

        for i in range(1, page_total + 1):
            print('get page : %s' % i)
            learn_resp = s.send(s.prepare_request(Request('GET', wurl % i)))
            resp_words = learn_resp.json()

            for w in resp_words["data"]["objects"]:
                w["content"] = '"' + w["content"] + '"'
                w['definition'] = '"' + w['definition'].replace('\n','').replace('\r','') + '"'
                w['definition'] = w['definition'].replace('""','"')
                writer.writerow(w)
                print('write row : %s,  %s' % (w['content'],w['definition']))
                #print(w)

# export_my_words("fw.csv", get_familiar_words)
# export_my_words("mw.csv", get_master_words)
# export_my_word_list('words_list')

threads = []
results = []
start = datetime.datetime.now()

url_queue = list(build_word_list_urls(s, words_urls))

for url in url_queue:
    threads.append(threading.Thread(target=export_my_word_list, args=(s, url, results)))

for t in threads:
    # t.setDaemon(True)
    t.start()
    t.join()

span = datetime.datetime.now() - start

words_set = set(results)

with open('words_list_{:%Y%m%d}.csv'.format(datetime.datetime.now()), 'w') as f:
    f.writelines([w + "\n" for w in words_set])

print('span:%s' % span)
